package han.bashe.web.servlet;

import han.bashe.common.bifang.BifangClient;
import han.bashe.common.cache.InnerCache;
import han.bashe.common.config.Config;
import han.bashe.common.entity.BifangEntityConstants;
import han.bashe.common.jms.JmsBinding;
import han.bashe.common.jms.JmsBindingWrapper;
import han.bashe.common.jms.NormalJms;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import v1.query.web.bifang.han.CatBindingDocument.CatBinding;
import v1.query.web.bifang.han.ServiceBusDocument.ServiceBus;
import v1.query.web.bifang.han.ServiceCatDocument.ServiceCat;

/**
 * Servlet implementation class for Servlet: InitServlet
 *
 */
 public class InitServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public InitServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		init();
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}   	  	  
	
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		try {
			Config.init();
			ServiceBus currentBus = BifangClient.getServiceBusInfoByCode(Config.getConfig().getBusCode());
			Config.setCurrentBusId(currentBus.getId());
			InnerCache.putServiceBus(currentBus);
			Config.setNotifyServiceCode(currentBus.getNotifyServiceCode());
			
			ServiceCat cat = BifangClient.getDefaultServiceCatalog();
			List<CatBinding> cbl =  cat.getCatBindingList();
			for(CatBinding cb : cbl){
				if(cb.getName().equals(BifangEntityConstants.CAT_BINDING_STATISTICS)){
					JmsBindingWrapper jbw = new JmsBindingWrapper();
					JmsBinding jb = new JmsBinding();
					jb.setServer(cb.getBinding().getJmsBinding().getServer());
					jb.setServerVersion(cb.getBinding().getJmsBinding().getServerVersion());
					jb.setJmsType(cb.getBinding().getJmsBinding().getJmsType());
					NormalJms nj = new NormalJms();
					nj.setDestinationJndi(cb.getBinding().getJmsBinding().getInfo().getNormalJms().getDestinationJndi());
					nj.setFactoryUrl(cb.getBinding().getJmsBinding().getInfo().getNormalJms().getFactoryUrl());
					nj.setServerUrl(cb.getBinding().getJmsBinding().getInfo().getNormalJms().getServerUrl());
					jbw.setInfo(nj);
					jbw.setJmsBinding(jb);
					Config.setStatisticsJmsBinding(jbw);
					break;
				
				}
			}
			
		} catch (Exception e) {
			Log logger = LogFactory.getLog(InitServlet.class);
			logger.error("InitServlet.init()", e);
			throw new ServletException(e);
		}
	}   
}